package com.uam.core.controller;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.uam.core.business.PointSignal;
import com.uam.core.scheduler.PointInfoScheduler;
import com.uam.core.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.uam.core.lk.utils.ResponseResult;
import com.uam.core.lk.utils.annotation.BaseErrorCode;
import com.uam.core.entity.enums.ErrorCode;
import com.uam.core.entity.model.ElasticsearchQueryResult.CommonFields;
import com.uam.core.entity.uitls.StringUtil;
import com.uam.core.service.BaseStationService;

import lombok.extern.slf4j.Slf4j;

/**
 * 基站管理
 *
 * @author lishf
 */
@Slf4j
@RestController
@RequestMapping("/uam/route/base_station")
public class BaseStationController {

    @Autowired
    private BaseStationService baseStationService = null;

	@Autowired
	private PointSignal pointSignal ;

    @GetMapping(value = "/detail", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
            MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE})
    public ResponseResult<Map<String, Object>> detail(@RequestParam(name = "id", required = false) String id) {
        long startTime = System.currentTimeMillis();
        try {
            if (StringUtil.isEmpty(id)) {
                BaseStationController.log.error("获取基站详情失败：id为空");
                return new ResponseResult<Map<String, Object>>(ErrorCode.ID_IS_ILLEGAL);
            }
            id = id.trim();
            if (StringUtil.isEmpty(id)) {
                BaseStationController.log.error("获取基站详情失败：id为空");
                return new ResponseResult<Map<String, Object>>(ErrorCode.ID_IS_ILLEGAL);
            }
            Map<String, Object> baseStation = this.baseStationService.detail(id);
            if (baseStation == null) {
                BaseStationController.log.error("获取基站详情失败：id不存在");
                return new ResponseResult<Map<String, Object>>(ErrorCode.ID_IS_NOT_EXIST);
            }
            ResponseResult<Map<String, Object>> response = new ResponseResult<Map<String, Object>>(baseStation);
            BaseStationController.log.info("base_station detail cost:{}ms", System.currentTimeMillis() - startTime);
            return response;
        } catch (Throwable e) {
            BaseStationController.log.error("获取基站详情异常", e);
            return new ResponseResult<Map<String, Object>>(BaseErrorCode.SpringExceptionErrorCode);
        }
    }

    @SuppressWarnings("deprecation")
    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = {
            MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
            MediaType.APPLICATION_OCTET_STREAM_VALUE})
    public ResponseResult<Boolean> update(@RequestBody Map<String, Object> data) {
        log.info("base_station update data:{}", JSONObject.toJSON(data));
        long startTime = System.currentTimeMillis();
        try {
            String id = (String) data.remove(CommonFields.ID.getName());
            if (StringUtil.isEmpty(id)) {
                BaseStationController.log.error("更新基站信息失败：id为空");
                return new ResponseResult<Boolean>(ErrorCode.ID_IS_ILLEGAL);
            }
            id = id.trim();
            if (StringUtil.isEmpty(id)) {
                BaseStationController.log.error("更新基站信息失败：id为空");
                return new ResponseResult<Boolean>(ErrorCode.ID_IS_ILLEGAL);
            }
            if (data.size() == 0) {
                BaseStationController.log.error("更新基站信息失败：无待更新的字段数据");
                return new ResponseResult<Boolean>(ErrorCode.NO_FIELD_DATA);
            }

            //之前就是关闭的
//			boolean flag=false;
//	        if(data.containsKey("status")){
//				if(data.get("status").equals("0")){
//					for (int i=0;i<PointInfoScheduler.signalStrength.size();i++){
//						Map<String, Object> d =PointInfoScheduler.signalStrength.get(i);
//						if(d.equals(id) && data.get("status").equals(d.get("status"))){
//							flag=true;
//							break;
//						}
//					}
//				}
//
//			}

            ResponseResult<Boolean> response = new ResponseResult<Boolean>(Boolean.valueOf(this.baseStationService.update(id, data)));
            //情况缓存
            PointInfoScheduler.signalStrength.clear();
            pointSignal.checkPointSignal(id);


            BaseStationController.log.info("base_station update cost:{}ms", System.currentTimeMillis() - startTime);
            return response;
        } catch (Throwable e) {
            BaseStationController.log.error(e.getMessage(), e);
            return new ResponseResult<Boolean>(BaseErrorCode.SpringExceptionErrorCode);
        }
    }

}
